
//index.js
//获取应用实例
var tcity = require("../../utils/citys.js");
var app = getApp()
Page({
  data: {
    provinces: [],
    province: "",
    citys: [],
    city: "",
    countys: [],
    county: '',
    value: [0, 0, 0],
    values: [0, 0, 0],
    area:[],
    flag:0,
    condition: false,
    openid: app.globalData.openid,
    source:'',
  },
  open: function (e) {
    if(this.data.flag==0){
      // console.log(e);
      // console.log('e.detail.value'+e.detail.value);
      this.setData({
        condition: !this.data.condition,
        area: e.detail.value,
        flag:1
      })
    }else{
      var add = this.data.province + this.data.city+this.data.county
      this.setData({
        condition: !this.data.condition,
        area: add,  
      })
      console.log(this.data.area);
    }
  },
  bindChange: function (e) {
    console.log(e);
    var val = e.detail.value
    var t = this.data.values;
    var cityData = this.data.cityData;
    if (val[0] != t[0]) {
      // console.log('province no ');
      const citys = [];
      const countys = [];
      for (let i = 0; i < cityData[val[0]].sub.length; i++) {
        citys.push(cityData[val[0]].sub[i].name)
      }
      for (let i = 0; i < cityData[val[0]].sub[0].sub.length; i++) {
        countys.push(cityData[val[0]].sub[0].sub[i].name)
      }
      this.setData({
        province: this.data.provinces[val[0]],
        city: cityData[val[0]].sub[0].name,
        citys: citys,
        county: cityData[val[0]].sub[0].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], 0, 0]
      })
      return;
    }
    if (val[1] != t[1]) {
      // console.log('city no');
      const countys = [];
      for (let i = 0; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
        countys.push(cityData[val[0]].sub[val[1]].sub[i].name)
      }
      this.setData({
        city: this.data.citys[val[1]],
        county: cityData[val[0]].sub[val[1]].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], val[1], 0]
      })
     
      return;
    }
    if (val[2] != t[2]) {
      // console.log('county no');
      this.setData({
        county: this.data.countys[val[2]],
        values: val
      })
      return;
    }
  },
  formSubmit: function (e) {
    console.log(this.data.area);
    console.log('form发生change事件，携带value值为：', e.detail.value.ausername);
    if (e.detail.value.ausername.length == 0 || e.detail.value.amobile.length == 0 || e.detail.value.addresss.length == 0) {
      wx.showToast({
        title: '收货人姓名、联系电话、详细地址,不能为空',
        icon: 'fail',
        duration: 2000,
        image: '../../images/tanhao.png'
      });
    } else {
      wx.request({
        url: getApp().data.apiurl + '/index.php/index/index/addaddress',
        data: {ausername:e.detail.value.ausername,
          amobile: e.detail.value.amobile,
          area: this.data.area,
          addresss: e.detail.value.addresss,
          openid: app.globalData.openid
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          wx.redirectTo({

            url: "../adress/adress",
          })
        }
      })
    }
  },
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '添加地址'
    })

    console.log(options);
    // console.log("onLoad");
    var that = this;
    this.data.source=options.p,
    tcity.init(that);
    var cityData = that.data.cityData;
    const provinces = [];
    const citys = [];
    const countys = [];
    for (let i = 0; i < cityData.length; i++) {
      provinces.push(cityData[i].name);
    }
    // console.log('省份完成');
    for (let i = 0; i < cityData[0].sub.length; i++) {
      citys.push(cityData[0].sub[i].name)
    }
    // console.log('city完成');
    for (let i = 0; i < cityData[0].sub[0].sub.length; i++) {
      countys.push(cityData[0].sub[0].sub[i].name)
    }
    that.setData({
      'provinces': provinces,
      'citys': citys,
      'countys': countys,
     
    })
 
    console.log('初始化完成');
  },
})
